package View.animation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyPanel extends JPanel implements ActionListener {

    private final int PANEL_WIDTH = 500;
    private final int PANEL_HEIGHT = 500;

    private Timer timer;
    private MovingItem movingItem;

    public MyPanel() {
        super();
        this.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
        this.setBackground(Color.lightGray);
        this.movingItem = new MovingItem(2, 1, 0, 0);
        timer = new Timer(50, this);
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(movingItem.getImage(), movingItem.getX(), movingItem.getY(), null);
        Toolkit.getDefaultToolkit().sync(); // for Linux user
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        movingItem.move();
        repaint();
    }
}
